package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.StoriesCat;

public class GenericStoriesCatDAO extends AutoKeys implements
		org.jetnuke.dao.StoriesCatDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericStoriesCatDAO.class);

	public StoriesCat retrieveByPK(int storyId) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryCatModel.selectByID"));
		p.setInt(1, storyId);

		ResultSet rs = p.executeQuery();
		StoriesCat s = new StoriesCat();

		if (rs.next()) {
			fillStoryCatFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillStoryCatFromResultSet(StoriesCat s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillStoryCatFromResultSet(StoriesCat, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCATID(rs.getInt("CATID"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillStoryCatFromResultSet(StoriesCat, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			StoriesCat u = new StoriesCat();
			fillStoryCatFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryCatModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#selectAllOrderTitle()
	 */
	public List selectAllOrderTitle() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTitle() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryCatModel.selectAllOrderTitle"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderTitle() - end"); //$NON-NLS-1$
		}
		return list;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#selectOrderCounterDesc(int)
	 */
	public List selectOrderCounterDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryCatModel.selectOrderCounterDesc"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderCounterDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#addNew(org.jetnuke.entities.StoriesCat)
	 */
	public int addNew(StoriesCat cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(StoriesCat) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("StoryCatModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("StoriesCatModel.lastGeneratedStoryCatID"));
		initNewStoryCat(cat, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(StoriesCat) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param cat
	 * @param p
	 */
	private void initNewStoryCat(StoriesCat cat, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewStoryCat(StoriesCat, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, cat.getTITLE());
		p.setInt(2, cat.getCOUNTER());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewStoryCat(StoriesCat, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#update(org.jetnuke.entities.StoriesCat)
	 */
	public void update(StoriesCat cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(StoriesCat) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryCatModel.update"));
		p.setInt(1, cat.getCOUNTER());
		p.setString(2, cat.getTITLE());
		p.setInt(3, cat.getCATID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(StoriesCat) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#deleteByPK(int)
	 */
	public void deleteByPK(int storyCatId) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryCatModel.deleteByID"));
		p.setInt(1, storyCatId);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesCatModel#incCounter(int)
	 */
	public void incCounter(int catid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryCatModel.incCounter"));
		p.setInt(1, catid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - end"); //$NON-NLS-1$
		}
	}

}
